//
//  DDVisitorView.swift
//  doudou_weibo
//
//  Created by apple on 2016/11/15.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

//定义一个协议
@objc protocol DDVisitorDelegate {
    
    func logIn()
    
    func register()
    
}

class DDVisitorView: UIView {
    
    //定义代理属性
    weak var delegate: DDVisitorDelegate?

    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI() {
        
        //设置视图背景颜色
        self.backgroundColor = UIColor.init(white: 237.0/255.0, alpha: 1)
        
        //添加
        addSubview(animationImageView)
        addSubview(backImageView)
        addSubview(houseImageView)
        addSubview(titleLabel)
        addSubview(logInButton)
        addSubview(registerButton)
        
        //约束
        animationImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        backImageView.snp.makeConstraints { (make) in
            make.center.equalTo(animationImageView)
        }
        houseImageView.snp.makeConstraints { (make) in
            make.center.equalTo(animationImageView)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(animationImageView.snp.bottom).offset(30)
            make.centerX.equalTo(self)
        }
        logInButton.snp.makeConstraints { (make) in
            make.right.equalTo(self.snp.centerX).offset(-20)
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.centerX).offset(20)
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        
        //按钮添加监听事件
        logInButton.addTarget(self, action: #selector(logInButtonDidClick), for: .touchUpInside)
        registerButton.addTarget(self, action: #selector(registerButtonDidClick), for: .touchUpInside)
        
    }
    
    @objc private func logInButtonDidClick() {
        print("登录按钮被点击了")
        //调用代理方法
        self.delegate?.logIn()
    }
    
    @objc private func registerButtonDidClick() {
        print("注册按钮被点击了")
        //调用代理方法
        self.delegate?.register()
    }
    
    //提供一个设置视图先内容的方法
    func upDataView(imageName: String, title: String, isHome: Bool = false) {
        
        animationImageView.image = UIImage.init(named: imageName)
        titleLabel.text = title
        
        if isHome {
            //执行动画
            setAnimation()
        }else{
            houseImageView.isHidden = true
            backImageView.isHidden = true
        }
        
    }
    
    //动画的方法
    private func setAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.duration = 1
        
        animation.isRemovedOnCompletion = false
        
        animationImageView.layer.add(animation, forKey: "donghua")
    }
    
    private lazy var animationImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    private lazy var houseImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    private lazy var backImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    private lazy var titleLabel: UILabel = {
       
        let label = UILabel(text: "我就随便写一句话", fontSize: UIFont.systemFont(ofSize: 14), textColor: UIColor.darkGray)
        label.textAlignment = .center
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = 220
        return label
    }()

    private lazy var logInButton: UIButton = {
        
        let button = UIButton(title: "登录", textColor: UIColor.darkGray, fontSize: UIFont.systemFont(ofSize: 15), imageName: "common_button_white")
        return button
    }()
    
    private lazy var registerButton: UIButton = {
      
        let button = UIButton(title: "注册", textColor: UIColor.orange, fontSize: UIFont.systemFont(ofSize: 15), imageName: "common_button_white")
        return button
    }()
    
}













